package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.PageList;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.BrowsingService;

/**
 * 验证检查类
 * @author wushubin
 *
 */
@Controller
public class BrowsingController extends BaseController3 {

	private static Logger log = LoggerFactory.getLogger(BrowsingController.class);
	private static final String EDIT_VIEW = "browsing/edit";
	private static final String UPDATE_VIEW = "browsing/update";
	private static final String ADD_VIEW = "browsing/add";
	private static final String LIST_VIEW = "browsing/list";
	private static final String DELETE_VIEW = "browsing/delete";
	private static final String HOME_VIEW = "browsing/index";
	
	private static final String EDIT = "/browsing/edit";
	private static final String UPDATE = "/browsing/update";
	private static final String ADD = "/browsing/add";
	private static final String LIST = "/browsing/list";
	private static final String DELETE = "/browsing/delete";
	private static final String HOME = "/browsing/index";
	
	@Resource
	private BrowsingService browsingService;
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	
	@RequestMapping(HOME)
    public String home(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 3);
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		PageList<Map<String,Object>> list = browsingService.getUserHistoryList(pageNo, pageSize, user.getId()+"");
    		model.addAttribute("list",list );
    		model.addAttribute("pageTurn",list.getPageTurn() );
		} catch (Exception e) {
			log.error("func[home] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return HOME_VIEW;
    }
	
	@RequestMapping(DELETE)
	public JSONObject deleteUserHistory(HttpServletRequest request) {
		try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 3);
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		int browsingId = reqInfo.getIntParameter(ApiUrlParamEnum.browsingId.getParamName(), -1);
    		JSONObject resultJson = browsingService.deleteUserBrowsing(pageNo, pageSize, user.getId(), browsingId);
    		return resultJson;
		} catch (Exception e) {
			log.error("func[deleteUserHistory] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
    	return null;
	}
	
}
